package com.socket.netty;

import com.socket.batch.collector.DataAnalysis;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import static java.lang.Integer.SIZE;
import static org.springframework.util.FileCopyUtils.BUFFER_SIZE;

/**
 * Created by yuan on 2017/7/14.
 */
public class SocketClient {
    public static void main(String[] args) {
        Socket client = null;
        BufferedInputStream reader = null;
        BufferedOutputStream writer = null;
        DataInputStream inputStream=null;
        BufferedReader br = null;
        StringBuilder strBuild=null;
        try {
            client = new Socket("127.0.0.1", 8085);
            //client = new Socket("127.0.0.1", 8085);
            writer = new BufferedOutputStream(client.getOutputStream());

            reader = new BufferedInputStream(client.getInputStream());

            //byte[] b = DataAnalysis.hex2Bytes("5B0000005A02000000021200000023030100000000001234D3E5413847323138203347000000000000000000000212020000002400120707E111033B0652B76801C7A2720000000000013EE3000001060000000200000000C2615D");
            byte[] b = DataAnalysis.hex2Bytes("5B000000480000125E02100100BC614E010001000000000000BC614E31323334353637383132372E302E302E3100000000000000000000000000000000000000000000002708F4505D");
            //byte[] b = DataAnalysis.hex2Bytes("5B00000048000001E0100100BC614E010001000000000000BC614E31323334353637383132372E302E302E3100000000000000000000000000000000000000000000002708EC705D");
            //System.out.println("客户端发送的消息是: " + new String(b, "UTF-8"));
            for(int i=0;i<10000000;i++){
                System.out.println("客户端发送的消息是: " + new String(b, "GB2312"));
                writer.write(b);
                writer.flush();

                inputStream = new DataInputStream(client.getInputStream());
                char[] data = new char[BUFFER_SIZE];
                br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                int len = br.read(data);
                String rexml = String.valueOf(data, 0, len);        //接收一个字符串数据
                System.out.println("rexml = [" + rexml + "]");
            }



        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
                if (br != null) {
                    br.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
